<?php
/**
 * 信息中心
 * Created by PhpStorm.
 * User: Earl <guo@seosite.cn>
 * Date: 2017/9/27
 * Time: 上午10:14
 * Updatetime:
 * updater：
 */

namespace WAdmin\Controller;

class MessageController extends BaseController
{


    /**
     * @desc 用户列表
     * @return array
     * @author Egor<http://gitee.com/seosite>
     */
    public function userList()
    {
        $userList = M('user')->field('id,username')->select();

        return showList($userList, new \stdClass());
    }

    /**
     * @desc 发送系统消息
     * @param $param
     * @return array
     * @author Egor<http://gitee.com/seosite>
     */
    public function sendSysMsg($param)
    {
        $check = CheckFactoryController::isLogin();
        $check->before($param);

        $check = CheckFactoryController::isParamSet();
        $check->before($param);

        $check = CheckFactoryController::isChildParamSet();
        $check->before('title,uid,content');

        unset($check);

        $uidsArr = explode(',', $param['uid']);
        if (!empty($uidsArr)) {
            foreach ($uidsArr as &$val) {
                if (!empty($val)) {
                    $insert_data = [
                        'uid' => $val,
                        'send_uid' => $this->userInfo->id,
                        'title' => $param['title'],
                        'content' => $param['content'],
                        'read_time' => '',
                        'is_read' => '0',
                        'type' => 1,
                        'create_time' => time(),
                        'warning_type' => ''
                    ];
                    $operation = M('message')->add($insert_data);
                }
            }
            unset($val);
            return showErrorMsg(L("_OPERATION_SUCCESS_"));
        }

        return showErrorMsg(L("_OPERATION_FAIL_"), 100002);

    }


    /**
     * @desc 消息列表
     * @param $param
     * @return array
     * @author Egor<http://gitee.com/seosite>
     */
    public function msgList($param)
    {
        $check = CheckFactoryController::isLogin();
        $check->before($param);

        $check = CheckFactoryController::isParamSet();
        $check->before($param);

        $check = CheckFactoryController::isChildParamSet();
        $check->before('type');

        unset($check);

        $p = empty($param['p']) ?: $param['p'];
        $listRows = empty($param['listRows']) ? 20 : $param['listRows'];


        $model = M('message');

        $map['uid'] = $this->userInfo->id;

        $map['type'] = $param['type'];

        list($list, $page) = _listPage($model, $map, $p, null, null, $listRows);

        if ($param['type'] == 2 && !empty($list)) {
            foreach ($list as $k => $v) {
                $list[$k]['warning_type']=$this->warningList($v['warning_type']);
            }
        }
        return showList($list, $page);

    }

    //报警消息类别
    public function warningList($k)
    {
        $list[1] = "出区域（围栏）报警";

        $list[2] = "入区域（围栏）报警";

        $list[3] = "电瓶拆除报警 （断电）";

        $list[4] = "温度超高报警";

        $list[5] = "温度超低报警";

        $list[6] = "湿度报警";

        $list[7] = "停车超时报警";

        $list[8] = "掉线报警";

        $list[9] = "上线报警";

        $list[10] = "电压低报警";

        $list[11] = "电压高报警";

        $list[12] = "开门报警";

        $list[13] = "制冷机开报警";

        $list[14] = "制冷机关报警";

        $list[15] = "油量低报警";

        $list[16] = "疲劳驾驶报警（行驶超过几个小时就报警）";

        $list[17] = "偏离线路报警（偏离围栏里的线型）";

        $list[18] = "GPS信号报警";


        return $list[$k];

    }


}